﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OnlineManagement.BLL;
namespace OnlineManagement.Web.pages
{
    public partial class SowUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // <a href="SowUpdate.aspx?hoggeryId=hoggeryId&&id=<%#Eval("SowID") %>&&bitId=<%#Eval("BitID") %>&&hybridizationCount=<%#Eval("HybridizationCount")%>
                //&&propagateCount=<%#Eval("PropagateCount")%>&&comment=<%#Eval("Comment")%>" title="修改" rel="modal">
                //string hoggerIdStr = Request.QueryString["hoggeryId"] ?? string.Empty;
               string hoggerIdStr =  Request.Cookies.Get(OnlineManagement.Common.PubConstant.HoggeryIdCookieName)==null?string.Empty
                    : Request.Cookies.Get(OnlineManagement.Common.PubConstant.HoggeryIdCookieName).Value;

                string bitId = Request.QueryString["bitId"] ?? string.Empty;
                string sowId = Request.QueryString["id"] ?? string.Empty;
                string pigPenId = Request.QueryString["pigPenId"] ?? string.Empty;
                string hybridizationCount = Request.QueryString["hybridizationCount"] ?? string.Empty;
                string propagateCount = Request.QueryString["propagateCount"] ?? string.Empty;
                string comment = Request.QueryString["comment"] ?? string.Empty;
                int hoggerId;
                if (int.TryParse(hoggerIdStr, out hoggerId))
                {
                    GetSowInfo(hoggerId, pigPenId);
                }

                lbBitID.Text = bitId;
                hfSowId.Value = sowId;
                lbPigPenId.Text = pigPenId;
                txthybridizationCount.Text = hybridizationCount;
                txtPropagateCount.Text = propagateCount;
                txtComment.Text = comment;

                //ddlPigPen.Visible = false;
                //lbtnChangeNum.Click += new EventHandler(lbtnChangeNum_Click);
                //ddlPigPen.SelectedIndexChanged += new EventHandler(ddlPigPen_SelectedIndexChanged);
            }
        }

        void ddlPigPen_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbBitID.Text = ddlPigPen.SelectedItem.Text;
        }

        void lbtnChangeNum_Click(object sender, EventArgs e)
        {
            ddlPigPen.Visible = true;
        }

        private void GetSowInfo(int hoggerId,string pigPenId)
        {
            
           DataSet ds = OnlineManagement.Common.DataCache.GetHogBaseInfo();
           if(ds==null)
            {
                ProcedureHandle handle = new ProcedureHandle();
                //return 3 tables,1:PigSource,2:PropagateState,3:PigBreed
                ds = handle.GetPigBaseInfoProcedure(hoggerId);
                OnlineManagement.Common.DataCache.CacheHogBaseInfo(ds);
            }

            if (ds != null && ds.Tables != null)
            {
                ddlPigPen.DataSource = ds.Tables[3];
                ddlPigPen.DataTextField = "Name";
                ddlPigPen.DataValueField = "ID";
                ddlPigPen.DataBind();
                ListItem item = new ListItem(pigPenId,pigPenId);
                ddlPigPen.Items.Insert(0, item);

                ddlPropagateState.DataSource = ds.Tables[1];
                ddlPropagateState.DataTextField = "Name";
                ddlPropagateState.DataValueField = "ID";
                ddlPropagateState.DataBind();
            }

        }
    }
}